#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>



int num=0;
sem_t mysem1;
sem_t mysem2;

void *myfunc1(void *arg)
{
	while(1)
	{
		sem_wait(&mysem1);
		printf("func1: num=%d\n",num++);
		sleep(1);
		sem_post(&mysem2);
	}
}

void *myfunc2(void *argc)
{	
	while(1)
	{
		sem_wait(&mysem2);
		printf("func2: num=%d\n",num++);
		sleep(1);
		sem_post(&mysem1);
	}
}

int main(int argc, const char *argv[])
{
	sem_init(&mysem1,0,1);
	sem_init(&mysem2,0,0);
	pthread_t ptid1,ptid2;
	pthread_create(&ptid1,NULL,myfunc1,NULL);
	pthread_create(&ptid2,NULL,myfunc2,NULL);

	pthread_detach(ptid1);
	pthread_detach(ptid2);	
	while(1)
	{

	}

	return 0;
}
